//
//  AFNetWorkService.m
//  TBRJL
//
//  Created by 程三 on 15/6/10.
//  Copyright (c) 2015年 程三. All rights reserved.
//

#import "AFNetWorkService.h"
#import "AFHTTPRequestOperationManager.h"
#import "Globle.h"


#import "AFURLSessionManager.h"


@implementation AFNetWorkService


-(void)requestWithServiceName:(NSString *)serviceName
                       params:(NSMutableDictionary *)params
                   httpMethod:(NSString *)httpMethod
           resultIsDictionary:(BOOL)resultIsDictionary
                completeBlock:(RequestCompelete)block
{
    
    NSString *serviceUrl = ServiceURL;
    
    //拼接URL
    NSString *url = [NSString stringWithFormat:@"%@/SZFGWAPP/restservices/Rsss/%@/query",serviceUrl,serviceName];
    if ([serviceName isEqualToString:@"fgwoa_getDwForAPP2"]) {
        url = [url stringByAppendingString:[NSString stringWithFormat:@"?key=%@",params[@"key"]]];
    }else if([serviceName isEqualToString:@"fgwoa_sendSmsCode"]){
        
        url = [url stringByAppendingString:[NSString stringWithFormat:@"?mobilephone=%@&userName=%@&jg=%@&bm=%@",params[@"mobilephone"],params[@"username"],params[@"firstcode"],params[@"secondcode"]]];
        
    }else if ([serviceName isEqualToString:@"fgwoa_loginValidate"]){
        
        url = [url stringByAppendingString:[NSString stringWithFormat:@"?shebeihao=%@&mobilephone=%@&userName=%@&yanzhengma=%@",params[@"shebeihao"],params[@"mobilephone"],params[@"username"],params[@"code"]]];
        
    }
    
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<>+"].invertedSet];
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    if(!resultIsDictionary)
    {
        manager.responseSerializer = [AFCompoundResponseSerializer serializer];//响应
    }
    else
    {
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    
    
    //设置相应内容类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/plain",@"text/javascript",nil];
    
    //处理POST的参数
    NSComparisonResult compareRet = [httpMethod caseInsensitiveCompare:@"POST"];
    if(NSOrderedSame == compareRet)
    {
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             
             NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
             if(block != nil)
             {
                 block(resultIsDictionary ? resultDic:operation.responseString);
             }
         }
              failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"调用服务出错(POST)");
             if(block != nil)
             {
                 block(nil);
             }
         }];
        
    }else{
        [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             
             NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
             
             if(block != nil)
             {
                 block(resultIsDictionary?resultDic:operation.responseString);
                 
             }
         }
             failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"调用服务出错(GET)");
             if(block != nil)
             {
                 block(nil);
             }
         }];
    }
}





-(void)RequestWithServiceName:(NSString *)serviceName
                       params:(NSMutableDictionary *)params
                   httpMethod:(NSString *)httpMethod
           resultIsDictionary:(BOOL)resultIsDictionary
                completeBlock:(RequestCompelete)block
{
    
    
    NSString *serviceUrl = ServiceURL;
    //拼接URL
    NSString *url = [NSString stringWithFormat:@"%@/SZFGWAPP/restservices/Rsss/%@/query",serviceUrl,serviceName];
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<>+"].invertedSet];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    if(!resultIsDictionary)
    {
        manager.responseSerializer = [AFCompoundResponseSerializer serializer];//响应
    }
    else
    {
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    
    
    //设置相应内容类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/plain",@"text/javascript",nil];
    
    
    //处理POST的参数
    NSComparisonResult compareRet = [httpMethod caseInsensitiveCompare:@"POST"];
    if(NSOrderedSame == compareRet)
    {
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
            
             NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
             if(block != nil)
             {
                 
                 block(resultIsDictionary ? resultDic:operation.responseString);
             }
         }
              failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"调用服务出错(POST)");
             if(block != nil)
             {
                 block(nil);
             }
         }];
    }
    else
    {
        [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
             if(block != nil)
             {
                 block(resultIsDictionary ? resultDic:operation.responseString);
                 
             }
         }
             failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"调用服务出错(GET)");
             if(block != nil)
             {
                 block(nil);
             }
         }];
    }
}




-(NSString*)objectToJson:(NSObject *)object
{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
